﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBO.Data;

namespace PBO.Game.BattleClass
{
    public partial class Pokemon
    {

        #region lv

        public int AttackLV
        {
            get { return BattleState.AbilityLv.AtkLv; }
        }
        public int DefenceLV
        {
            get { return BattleState.AbilityLv.DefLv; }
        }
        public int SpeedLV
        {
            get { return BattleState.AbilityLv.SpeedLv; }
        }
        public int SpAttackLV
        {
            get { return BattleState.AbilityLv.SpAtkLv; }
        }
        public int SpDefenceLV
        {
            get { return BattleState.AbilityLv.SpDefLv; }
        }

        #endregion

        /// <summary>
        /// 获取回避
        /// </summary>
        /// <param name="target"></param>
        /// <param name="ignoreEvasionLvUp"></param>
        /// <returns></returns>
        public double GetEvasion(Pokemon target, bool ignoreEvasionLvUp)
        {
            int evaLV = BattleState.AbilityLv.EvasionLv;
            if (GetSelTrait() == Trait.Simple && target.GetSelTrait() != Trait.MoldBreaker)
                evaLV *= 2;
            if (evaLV > 6)
                evaLV = 6;
            if (evaLV < -6)
                evaLV = -6;

            if (ignoreEvasionLvUp && evaLV > 0)
            {
                evaLV = 0;
            }

            int accLV = target.BattleState.AbilityLv.AccuracyLv;
            if (target.GetSelTrait() == Trait.Simple)
                accLV *= 2;
            if (accLV > 6)
                accLV = 6;
            if (accLV < -6)
                accLV = -6;

            int lv = evaLV - accLV;
            if (BattleState.miracleEye || BattleState.foresight)
                lv = -accLV;

            if (MyTeam.ground.Unaware)
                lv = 0;

            if (lv > 0)
            {
                return 3 / (lv + 3);
            }
            else if (lv < 0)
            {
                return (3 - lv) / 3;
            }
            return 1;
        }

        /// <summary>
        /// 获取CT等级
        /// </summary>
        public double GetCTOdds(bool moveEffect)
        {
            int lv = BattleState.CTLV;
            var trait = this.GetSelTrait();
            var item = this.GetItem();
            if (trait == Trait.SuperLuck) lv++;

            if ((item == Item.LuckyPunch && this.Data.Number == 113) || (item == Item.Stick && this.Data.Number == 83))
            {
                lv += 2;
            }
            else if (item == Item.ScopeLens || item == Item.RazorClaw)
            {
                lv++;
            }
            if (moveEffect) lv++;
            int[] CTLVS = { 16, 8, 4, 3, 2 };
            return 1.0 / CTLVS[lv];
        }

        /// <summary>
        /// CT等级提升
        /// </summary>
        /// <param name="upCount"></param>
        /// <returns></returns>
        public bool CTLVUp(int upCount)
        {
            bool result = BattleState.CTLV < 4;
            if (result)
                BattleState.CTLV = Math.Min(4, BattleState.CTLV + upCount);
            return result;
        }

        #region lv up
        //public bool AtkLVUp(int upCount, bool showFailInfo = true)
        //{
        //    if (BattleState.AttackLV < 6)
        //    {
        //        BattleState.AttackLV += upCount;
        //        if (BattleState.AttackLV > 6)
        //            BattleState.AttackLV = 6;
        //        if (upCount > 1)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "物攻疯狂提升！");
        //        }
        //        else
        //        {
        //            MyTeam.log.AddText(GetNameString() + "物攻提升！");
        //        }
        //        return true;
        //    }
        //    else
        //    {
        //        if (showFailInfo)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "的物攻已经到达颠峰");
        //        }
        //        return false;
        //    }
        //}
        //public bool DefLVUp(int upCount, bool showFailInfo = true)
        //{
        //    if (BattleState.DefenceLV < 6)
        //    {
        //        BattleState.DefenceLV += upCount;
        //        if (BattleState.DefenceLV > 6)
        //            BattleState.DefenceLV = 6;
        //        if (upCount > 1)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "物防疯狂提升！");
        //        }
        //        else
        //        {
        //            MyTeam.log.AddText(GetNameString() + "物防提升！");
        //        }
        //        return true;
        //    }
        //    else
        //    {
        //        if (showFailInfo)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "的物防已经到达颠峰");
        //        }
        //        return false;
        //    }
        //}
        //public bool SpeedLVUp(int upCount, bool showFailInfo = true)
        //{
        //    if (BattleState.SpeedLV < 6)
        //    {
        //        BattleState.SpeedLV += upCount;
        //        if (BattleState.SpeedLV > 6)
        //            BattleState.SpeedLV = 6;
        //        if (upCount > 1)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "速度疯狂提升！");
        //        }
        //        else
        //        {
        //            MyTeam.log.AddText(GetNameString() + "速度提升！");
        //        }
        //        return true;
        //    }
        //    else
        //    {
        //        if (showFailInfo)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "的速度已经到达颠峰");
        //        }
        //        return false;
        //    }
        //}
        //public bool SAtkLVUp(int upCount, bool showFailInfo = true)
        //{
        //    if (BattleState.SpAttackLV < 6)
        //    {
        //        BattleState.SpAttackLV += upCount;
        //        if (BattleState.SpAttackLV > 6)
        //            BattleState.SpAttackLV = 6;
        //        if (upCount > 1)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "特攻疯狂提升！");
        //        }
        //        else
        //        {
        //            MyTeam.log.AddText(GetNameString() + "特攻提升！");
        //        }
        //        return true;
        //    }
        //    else
        //    {
        //        if (showFailInfo)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "的特攻已经到达颠峰");
        //        }
        //        return false;
        //    }
        //}
        //public bool SDefLVUp(int upCount, bool showFailInfo = true)
        //{
        //    if (BattleState.SpDefenceLV < 6)
        //    {
        //        BattleState.SpDefenceLV += upCount;
        //        if (BattleState.SpDefenceLV > 6)
        //            BattleState.SpDefenceLV = 6;
        //        if (upCount > 1)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "特防疯狂提升！");
        //        }
        //        else
        //        {
        //            MyTeam.log.AddText(GetNameString() + "特防提升！");
        //        }
        //        return true;
        //    }
        //    else
        //    {
        //        if (showFailInfo)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "的特防已经到达颠峰");
        //        }
        //        return false;
        //    }
        //}
        //public bool EvasionLVUp(int upCount, bool showFailInfo = true)
        //{
        //    if (BattleState.evasionLV < 6)
        //    {
        //        BattleState.evasionLV += upCount;
        //        if (BattleState.evasionLV > 6)
        //            BattleState.evasionLV = 6;
        //        MyTeam.log.AddText(GetNameString() + "回避提升！");
        //        return true;
        //    }
        //    else
        //    {
        //        if (showFailInfo)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "的回避已经到达颠峰");
        //        }
        //        return false;
        //    }
        //}
        //public bool AccuracyLVUp(int upCount, bool showFailInfo = true)
        //{
        //    if (BattleState.AccLV < 6)
        //    {
        //        BattleState.AccLV += upCount;
        //        if (BattleState.AccLV > 6)
        //            BattleState.AccLV = 6;
        //        MyTeam.log.AddText(GetNameString() + "命中提升！");
        //        return true;
        //    }
        //    else
        //    {
        //        if (showFailInfo)
        //        {
        //            MyTeam.log.AddText(GetNameString() + "的命中已经到达颠峰");
        //        }
        //        return false;
        //    }
        //}
        #endregion

#warning AbilityLvDown testing

        #region AbilityLvChanged

        public void AbilityLvDownChanged(StatType stat, Pokemon target, int value, bool showFailed = true)
        {
            if (!MyTeam.Group.Mist && CanAbilityDown(stat, target, showFailed))
            {
                AbilityLvDown(stat, value, showFailed);
            }
        }

        public void AbilityLvDownChanged(StatType stat, int value, bool showFailed = true)
        {
            if (!MyTeam.Group.Mist && CanAbilityDown(stat, null, showFailed))
            {
                AbilityLvDown(stat, value, showFailed);
            }
        }

        public void AbilityLvUpChanged(StatType stat, int value, bool showFailed = true)
        {
            AbilityLvUp(stat, value, showFailed);
        }

        private void AbilityLvUp(StatType stat, int value, bool showFailed)
        {
            var abilityLv = BattleState.AbilityLv;
            if (abilityLv[stat] >= 6)
            {
                abilityLv[stat] = 6;
                MyTeam.log.AppendMsg("7DMax", this.GetNameString(), stat);
                //if (showFailed)
                //{
                //    MyTeam.log.AppendMsg("7DMax", this.GetNameString(), stat);
                //}
            }
            else
            {
                value = Math.Abs(value);
                value = Math.Min(value, 6 - abilityLv[stat]);
                abilityLv[stat] += value;
                value = Math.Min(3, value);
                MyTeam.log.AppendMsg("7DUp" + value, this.GetNameString(), stat);
            }
        }

        private void AbilityLvDown(StatType stat, int value, bool showFailed)
        {
            var abilityLv = BattleState.AbilityLv;
            if (abilityLv[stat] <= -6)
            {
                abilityLv[stat] = -6;
                MyTeam.log.AppendMsg("7DMin", this.GetNameString(), stat);
                //if (showFailed)
                //{
                //    MyTeam.log.AppendMsg("7DMin", this.GetNameString(), stat);
                //}
            }
            else
            {
                value = Math.Abs(value);
                abilityLv[stat] = abilityLv[stat] - value;
                abilityLv[stat] = Math.Max(-6, abilityLv[stat]);
                value = Math.Min(3, value);
                MyTeam.log.AppendMsg("7DDown" + value, this.GetNameString(), stat);
                if (GetItem() == Item.WhiteHerb && BattleState.AbilityLv[stat] < 0) RaiseItem();
            }
        }

        private bool CanAbilityDown(StatType stat, Pokemon target, bool showFailed)
        {
            bool result = true;
            var trait = this.GetSelTrait();
            bool selfAffect = target != null && target.GetSelTrait() == Trait.MoldBreaker;

            if (!selfAffect)
            {
                if (trait == Trait.WhiteSmoke || trait == Trait.ClearBody)
                {
                    result = false;
                    if (showFailed)
                    {
                        MyTeam.log.AppendMsg("Ability", this.GetNameString(), this.GetSelTrait());
                        MyTeam.log.AppendMsg("7DLockAll", this.GetNameString());
                    }
                }
                else if ((trait == Trait.KeenEye && stat == StatType.Accuracy) || (trait == Trait.HyperCutter && stat == StatType.Atk))
                {
                    result = false;
                    if (showFailed)
                    {
                        MyTeam.log.AppendMsg("Ability", this.GetNameString(), this.GetSelTrait());
                        MyTeam.log.AppendMsg("7DLock", this.GetNameString(), stat);
                    }
                }
            }
            return result;
        }

        #endregion

        public void Haze()
        {
            BattleState.AbilityLv.SetAll(0);
            BattleState.StockpileDefCounter = 0;
            BattleState.StockpileSDefCounter = 0;
        }

    }
}
